#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include<QShortcut>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //------菜单栏------
    QMenuBar* menuBar = this->menuBar();
    this->setMenuBar(menuBar);

    QMenu* menu1 = new QMenu("文件(&F)");
    QMenu* menu2 = new QMenu("编辑(&E)");
    menuBar->addMenu(menu1);
    menuBar->addMenu(menu2);
    //----选项----
    QAction* act1 = new QAction("打开");
    QAction* act2 = new QAction("新建");
    QAction* act3 = new QAction("关闭(&C)");

    //---分割线
    menu1->addAction(act1);
    menu1->addSeparator();
    menu1->addAction(act2);
    menu1->addSeparator();
    menu1->addAction(act3);

    connect(act3,&QAction::triggered,this,&MainWindow::close);
    act1->setShortcut(QKeySequence("alt+o"));//alt+o
    act2->setShortcut(QKeySequence("alt+n"));//alt+n

    connect(act1,&QAction::triggered,this,&MainWindow::open);
    connect(act2,&QAction::triggered,this,&MainWindow::ActNew);

    //-----子菜单------

    QMenu* childMenu = new QMenu("子菜单");
    menu1->addMenu(childMenu);
    childMenu->addAction(act3);


    //----添加图标--
    QIcon icon = QIcon(":/pic.jpg");
    menu1->setIcon(icon);
    act1->setIcon(icon);
    childMenu->setIcon(icon);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::open()
{
    qDebug()<<"open is run";
}

void MainWindow::ActNew()
{
    qDebug()<<"new is run";
}

//void MainWindow::close()
//{
//    this->close();
//}

